/*
  Copyright © 2010  Camilo Costa Campos

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */


//Tags used in Touche:
//<ToucheText> "Text message to the other users" </ToucheText>
//<ToucheObject> </ToucheObject>
//<StartEditing> objId = int Id </ToucheStartEditing>
//<ToucheFinishEditing> objId = int Id </ToucheFinishEditing>
//

#include "interpreter.h"
#include <QXmlStreamReader>
#include <QXmlStreamAttributes>
#include <iostream>
#include <QDebug>

Interpreter::Interpreter()
{
}

void Interpreter::onMessageReceived(const QXmppMessage &msg)
{
    QXmlStreamAttributes attrs;
    QXmlStreamReader xml(msg.body());

    while(!xml.atEnd())
    {
        attrs = xml.attributes();

        switch(xml.readNext()){
        case QXmlStreamReader::Invalid:
            qWarning("erro encontrado");
            qWarning("%s", qPrintable(xml.errorString()));
            break;
        case QXmlStreamReader::StartDocument:
        case QXmlStreamReader::EndDocument:
        case QXmlStreamReader::EndElement:
            continue;
        default:;
        }

        if(xml.isStartElement())
        {
            if(xml.name().toString()== "ChatText")
            {
                xml.readNext();

                emit chatText(xml.text().toString(),msg.from());

                return;
            }

            if(xml.name().toString() == "PrivateText")
            {
                xml.readNext();

                emit privateText(xml.text().toString(),msg.from());

                return;
            }

            if(xml.name().toString()== "Element")
            {
                attrs = xml.attributes();
                xml.readNext();

                QUuid uuid = attrs.value("uuid").toString();
                QString type = attrs.value("type").toString();
                QString data = xml.text().toString();

                // TODO: receber arquivos
                emit elementReceived(uuid, type, data, QString());
                //chamar a função que bloqueia a edição do objeto cujo id é  xml.text().toString();
                return;

            }

            if(xml.name().toString()== "LockElement")
            {
                attrs = xml.attributes();
                xml.readNext();
                QUuid uuid = attrs.value("uuid").toString();

                emit elementLocked(uuid);
                return;

            }

            if(xml.name().toString() == "UnlockElement")
            {
                attrs = xml.attributes();
                xml.readNext();
                QUuid uuid = attrs.value("uuid").toString();

                emit elementUnlocked(uuid);
                return;
            }


            if(xml.name().toString() == "NewLink")
            {
                attrs = xml.attributes();
                xml.readNext();

                QUuid uuid1 = attrs.value("uuid1").toString();
                QUuid uuid2 = attrs.value("uuid2").toString();

                emit linkReceived(uuid1, uuid2);
                return;
            }

            if(xml.name().toString() == "KillElement"){
                attrs = xml.attributes();
                xml.readNext();

                QUuid uuid = attrs.value("uuid").toString();

                emit elementKilled(uuid);
                return;
            }

            if(xml.name().toString() == "KillLink"){
                attrs = xml.attributes();
                xml.readNext();

                QUuid uuid1 = attrs.value("uuid1").toString();
                QUuid uuid2 = attrs.value("uuid2").toString();

                emit linkKilled(uuid1, uuid2);
                return;
            }
        }
    }
}
